package xfuzzy.alg_genetico.indeterminado.output;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

public class ConfigurationHandler {

	public static synchronized void writeConfiguration(String output, String nameFile)  {
		
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new FileWriter(nameFile, true));
		    out.write(output + System.getProperty("line.separator"));
		    out.close();
		} 
		catch (IOException e) {
			System.out.println("Error: " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	public static synchronized HashMap<String, Double> getAllConfigurations(String nameFile)  {
		
		HashMap<String, Double> configurations = new HashMap<String, Double>();
		BufferedReader in = null;
		try  {
			
			in = new BufferedReader(new FileReader(nameFile));
			String line = in.readLine();
			String line1 = "";
			String line2 = "";
			String [] lines = null;
			// Cadena a leer
			// -5-4-3-2-10 : 0.130648547945249
			while (line != null)  {
				lines = line.split(" : ");
				line1 = lines[0];
				line2 = lines[1];
				configurations.put(line1, new Double(line2));
				line = in.readLine();
			}
		}
		catch(IOException e)  {
			System.out.println("Error: " + e.getMessage());
			e.printStackTrace();
		}
		return configurations;
	}
}
